﻿using CodeToolBox.Form;
using CodeToolBox.Form.UpdateForm;
using Common;
using Common.Entity;
using Common.Utility;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
namespace CodeToolBox
{
    public partial class MainForm : UIForm
    {
        public MainForm()
        {
            InitializeComponent();
        }
        private UpData updown;
        private async void MainForm_Load(object sender, EventArgs e)
        {
            foot_Version.Text = $"当前版本{Config.Version}";
            foot_newVersion.Text = "  \t\t\t\t                                                        ";
            CheckUpdate asdad = new CheckUpdate();
            updown = await asdad.CheckVersion();
            if (updown != null)
            {
                foot_newVersion.IsLink = true;
                foot_newVersion.Text = $"最新版本{updown.Version}";
                foot_newVersion.Click += Foot_newVersion_Click;

                //查看配置是否存在配置文件,是否弹窗提示
                bool upshow = GetConfig(Config.Version);

                if (upshow == false)
                {
                    UpdataCheckForm f11 = new UpdataCheckForm();
                    f11.labMessage.Text = $"亲爱的用户你好：\n \t\t 你当前的系统版本是{Config.Version}，最新版本是{updown.Version}。您可以点击主页右下角更新到最新版。";

                    var result = f11.ShowDialog();
                    UpdateFormshowResuly(result, Config.Version, f11.CheckBox1.Checked);
                }



            }




            //建议用封装的方法Add
            List<ObjectEntity> listobject = jiazai();

            foreach (ObjectEntity item in listobject)
            {
                UIFlowItem btn = new UIFlowItem();
                btn.descstr = item.ToolDesc;
                btn.objectName = item.ToolName;
                btn.picimage = item.ToolImg;
                btn.CustomControlClicked += (sender1, e1) =>
                {
                    FormClick(item.formName, item.ToolName);
                };
                uiFlowLayoutPanel1.Add(btn);
            }


        }


        private void UpdateFormshowResuly(DialogResult result, string version, bool checkstr)
        {
            try
            {
                string fileurl = Config.ObjectInIUrl;
                INIHelper.INIWrite("更新模块", $"Noshow_{version}", checkstr.ToString(), fileurl);
            }
            catch (Exception ex)
            {

                ShowErrorNotifier(ex.Message, true, 5000);
            }
        }


        /// <summary>
        /// 跳转页面
        /// </summary>
        /// <param name="formname"></param>
        private void FormClick(string formname, string formText)
        {
            try
            {
                // 遍历所有打开的窗体
                foreach (UIForm form in Application.OpenForms)
                {
                    if (form.Name == formname)
                    {
                        // 如果找到窗体则显示它并跳出循环
                        form.Focus();
                        form.StartPosition = FormStartPosition.CenterScreen;
                        return;
                    }
                }

                Type formType = Assembly.GetExecutingAssembly().GetTypes()
                    .FirstOrDefault(t => t.Name == formname);

                if (formType != null && formType.IsSubclassOf(typeof(UIForm)))
                {
                    UIForm f1 = (UIForm)Activator.CreateInstance(formType);
                    f1.Text = formText;
                    f1.Show();
                }
                else
                {
                    throw new Exception($"未找到页面{formname}");
                }
            }
            catch (Exception ex)
            {
                ShowErrorNotifier(ex.Message, true, 5000);
            }
        }



        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Foot_newVersion_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateForm f1 = new UpdateForm();
                f1.uiLabel2.Text = $"当前版本:{Config.Version}->更新版本:{updown.Version}";
                f1.Tag = updown;
                f1.ShowDialog();
            }
            catch (Exception ex)
            {

                ShowErrorNotifier(ex.Message, true, 5000);
            }
        }



        private List<ObjectEntity> jiazai()
        {
            List<ObjectEntity> objectEntities = new List<ObjectEntity>();
            try
            {

                ObjectEntity pwdEntity = new ObjectEntity()
                {
                    ToolName = "密码加密-解密",
                    ToolDesc = "针对于密码的加密和解密",
                    ToolImg = Properties.Resources.密码加密解密,
                    formName = "ToolPasswordForm"
                };
                objectEntities.Add(pwdEntity);

                ObjectEntity filepartzhEntity = new ObjectEntity()
                {
                    ToolName = "FielPart模板转换",
                    ToolDesc = "老平台FieldPart提取字段+新平台FieldPart模板生成。",
                    ToolImg = Properties.Resources.befam转换,
                    formName = "FieldPartZhForm"
                };
                objectEntities.Add(filepartzhEntity);

                ObjectEntity pwdentity2 = new ObjectEntity()
                {
                    ToolName = "Schema描述对比",
                    ToolDesc = "可以复制老平台Schema在GridView上按键Ctrl+V，点击对比可以完成新平台的更新！",
                    ToolImg = Properties.Resources.Schema描述对比,
                    formName = "SchemaDec"
                };
                objectEntities.Add(pwdentity2);

                ObjectEntity stringSetForm = new ObjectEntity()
                {
                    ToolName = "格式转换",
                    ToolDesc = @"格式转换
                    ,[BT_TYPE]
                    ,[BT_DATA1]
                    ,[BT_DATA2]
                    ,[BT_DATA3]转[BT_TYPE],BT_DATA1],[BT_DATA2],[BT_DATA3]",
                    ToolImg = Properties.Resources.格式转换,
                    formName = "StringSetForm"
                };
                objectEntities.Add(stringSetForm);


                ObjectEntity menuDBForm = new ObjectEntity()
                {
                    ToolName = "菜单对比",
                    ToolDesc =@"填入文本格式：
                    字段1
                    字段2
                    字段3
                    字段4
                    ...
                    ",
                    ToolImg = Properties.Resources.菜单对比,
                    formName = "MenuDBForm"
                };
                objectEntities.Add(menuDBForm);


            }
            catch (Exception ex)
            {
                ShowErrorNotifier(ex.Message, true, 5000);
            }
            return objectEntities;
        }

        private bool GetConfig(string version)
        {
            bool result = false;
            try
            {
                string fileurl = Config.ObjectInIUrl;
                if (File.Exists(fileurl))
                {
                    result = bool.Parse(INIHelper.INIRead("更新模块", $"Noshow_{version}", fileurl));
                }
                else
                {
                    string folderPath = fileurl.Split((fileurl.Split("\\").Last()))[0];
                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                    }
                }
            }
            catch (Exception ex)
            {
                ShowSuccessTip(ex.Message, 1500);
            }
            return result;
        }

    }
}
